package com.example.spring1.service.iml;

import com.example.spring1.entity.User;
import com.example.spring1.service.Userservice;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@Service
public class Userserviceiml implements Userservice {
    private final JdbcTemplate jdbcTemplate;
    public Userserviceiml(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public int addUser(User user) {
        String sql="insert into user(username,password,phone,email) values(?,?,?,?)";
        return jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getPhone());
    }

    @Override
    public int updateUser(User user) {
        String sql="update user set password=?,phone=?,email=? where username=?";
        return jdbcTemplate.update(sql,user.getPassword(),user.getPhone(),user.getEmail(),user.getUsername());
    }

    @Override
    public int deleteUser(User user) {
        String sql="delete from user where username=?";
        return jdbcTemplate.update(sql,user.getUsername());
    }
    @Override
    public List<User> getUsers() {
        String sql="select * from user";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
    }
}
